package it.controllocredito.frontend.activities;

import java.util.ArrayList;

import it.controllocredito.R;
import it.controllocredito.backend.persistence.CalllogPersistenceManager;
import it.controllocredito.backend.persistence.DatabaseHandler;
import it.controllocredito.backend.persistence.tables.RegistroChiamateTable;
import it.controllocredito.frontend.fragments.InternetTabFragment;
import it.controllocredito.frontend.fragments.SmsTabFragment;
import it.controllocredito.frontend.fragments.SoglieTabFragment;
import it.controllocredito.frontend.fragments.CallTabFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class MainFragmentActivity extends SherlockFragmentActivity {
    ViewPager mViewPager;
    TabsAdapter mTabsAdapter;
    TextView tabCenter;
    TextView tabText;
    ActionBar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);
        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mActionBar.setDisplayOptions(10, ActionBar.DISPLAY_SHOW_TITLE);
        mActionBar.setTitle("Controllo Credito");

        
        mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(mActionBar.newTab().setText("Soglie"), 
            SoglieTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Call"), 
        		CallTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Sms"), 
        		SmsTabFragment.class, null);
        mTabsAdapter.addTab(mActionBar.newTab().setText("Dati"), 
        		InternetTabFragment.class, null);
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getSupportMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
      return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	CalllogPersistenceManager calllogPersistenceManager;
      switch (item.getItemId()) {
	      case R.id.mn_params:
	    	Intent openParamsActivity = new Intent("it.controllocredito.frontend.activities.PARAMSACTIVITY");
	    	openParamsActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(openParamsActivity);
			overridePendingTransition(R.anim.slide_right,R.anim.slide_right);
	        break;
	      case R.id.mn_info:
	        Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
	            .show();
	        break;	        
	      case R.id.mn_dropTable:
				Toast.makeText(this,
						"Distruggo tutte le tabelle",
						Toast.LENGTH_LONG).show();
				
				DatabaseHandler databaseHandler = new DatabaseHandler(
						this);
				
				databaseHandler.dropTable(RegistroChiamateTable.getName());
				//databaseHandler.dropTable(MonitorTable.getName());
				break;
			case R.id.mn_create_update_table:
				Toast.makeText(this,
						"Creo e aggiorno tutte le tabelle",
						Toast.LENGTH_LONG).show();
				
				calllogPersistenceManager = new CalllogPersistenceManager(this.getApplicationContext());
				
				calllogPersistenceManager.cleanRegistro();
				
				calllogPersistenceManager.aggiornamentoRegistro();
				
				/*
				databaseHandler.addRecord(RegistroChiamateTable.getName(),
						RegistroChiamateTable.createRecord(
								CallLog.Calls.INCOMING_TYPE, 6000, "+39",
								"3477229171"));
				*/
				
				// databaseHandler.getRecordsRegistroChiamate();
				break;
			case R.id.mn_inizializza_table_from_registro:	
				calllogPersistenceManager = new CalllogPersistenceManager(this.getApplicationContext());
				
				
				calllogPersistenceManager.aggiornamentoRegistro();
				
				
				break;
			default:
				break;
			}

      return true;
    }
	
    public static class TabsAdapter extends FragmentPagerAdapter
            implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext,
                info.clss.getName(), info.args);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            Object tag = tab.getTag();
            for (int i = 0; i < mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }
}